/*
 * RecommendGoodsAdapter2.java
 * business
 *
 * Created by ChenTao on 2016/10/10.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.yidiandao.business.R;
import com.yidiandao.business.data.bean.Goods;
import com.yidiandao.business.ui.goodsdetail.GoodsDetailActivity;
import com.yidiandao.business.ui.main.home.HomeFragment;
import com.yidiandao.business.ui.main.shoppingcart.ShoppingCartFragment;
import com.yidiandao.business.utils.ImageLoader;
import com.yidiandao.business.utils.UmengUtils;

import java.util.List;

/** 购物车为空时推荐商品-Adapter */
public class RecommendGoodsAdapter extends RecyclerView.Adapter<RecommendGoodsAdapter.ViewHolder> {

    private Activity mActivity;

    private List<Goods> mGoodsList;

    private String mFragmentTag;

    public RecommendGoodsAdapter(Activity activity, String fragmentTag) {
        mActivity = activity;
        mFragmentTag = fragmentTag;
    }

    public void setData(List<Goods> goodsList) {
        mGoodsList = goodsList;
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View rootView =
                LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.item_recommend_goods, parent, false);

        return new ViewHolder(rootView);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        final Goods goods = mGoodsList.get(position);

        holder.tvGoodsName.setText(goods.getGoodsName());
        holder.tvGoodsPrice.setText(goods.getShopPrice());
        ImageLoader.loadImageWithPlaceHolder(mActivity, holder.ivImage, goods.getGoodsThumb());

        holder.ivImage.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (ShoppingCartFragment.TAG.equals(mFragmentTag)) {
                            UmengUtils.onEvent(
                                    mActivity, R.string.umeng_shopping_cart_click_recommend);
                        } else if (HomeFragment.TAG.equals(mFragmentTag)) {
                            UmengUtils.onEvent(mActivity, R.string.umeng_home_recommend_goods);
                        }
                        GoodsDetailActivity.actionStart(mActivity, goods.getGoodsId());
                    }
                });

//        holder.tvGoodsPrice.setOnClickListener(
//                new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        if (!PriceUtils.isLogin()) {
//                            LoginActivity.actionStart(mActivity);
//                        }
//                    }
//                });
    }

    @Override
    public int getItemCount() {
        return mGoodsList == null ? 0 : mGoodsList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        private ImageView ivImage;

        private TextView tvGoodsName;

        private TextView tvGoodsPrice;

        private ViewHolder(View itemView) {
            super(itemView);

            ivImage = (ImageView) itemView.findViewById(R.id.iv_goods_image);
            tvGoodsName = (TextView) itemView.findViewById(R.id.tv_goods_name);
            tvGoodsPrice = (TextView) itemView.findViewById(R.id.tv_goods_price);
        }
    }
}
